package fun.qu_an.minecraft.voicechatplugin;

import ax.arm32.minecraft.tickstasis.TickStasis;
import de.maxhenkel.voicechat.Voicechat;
import de.maxhenkel.voicechat.api.Group;
import de.maxhenkel.voicechat.api.VoicechatConnection;
import de.maxhenkel.voicechat.api.VoicechatPlugin;
import de.maxhenkel.voicechat.api.VoicechatServerApi;
import de.maxhenkel.voicechat.api.events.EventRegistration;
import de.maxhenkel.voicechat.api.events.MicrophonePacketEvent;
import de.maxhenkel.voicechat.api.events.VoicechatServerStartingEvent;
import de.maxhenkel.voicechat.plugins.impl.events.VoicechatServerStartingEventImpl;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.scoreboard.AbstractTeam;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.file.Path;
import java.util.List;

public class Qu_anVoiceChatPlugin implements ModInitializer, VoicechatPlugin {
	public static final Logger LOGGER = LogManager.getLogger();
	public static final String MOD_ID = "qu_an_voice_chat_plugin";
	public static final Path DATA_FOLDER = FabricLoader.getInstance().getConfigDir().resolve("voicechat");
	private static MinecraftServer minecraftServer;
	@Override
	public void onInitialize() {
		MultiFuncChatConfig.load();
		ServerLifecycleEvents.SERVER_STARTING.register(server
			-> Qu_anVoiceChatPlugin.minecraftServer = server);
		CommandRegistrationCallback.EVENT.register((commandDispatcher, b) -> {
			MultiFuncChatCommand.register(commandDispatcher);
		});
	}

	@Override
	public String getPluginId() {
		return MOD_ID;
	}

	@Override
	public void registerEvents(EventRegistration registration) {
		registration.registerEvent(MicrophonePacketEvent.class, this::onMicrophonePacket);
		System.out.println(MOD_ID + " loaded!");
	}

	private void onMicrophonePacket(MicrophonePacketEvent event) {
		if (!MultiFuncChatConfig.get().isEnable()) {
			return;
		}

		VoicechatConnection senderConnection = event.getSenderConnection();
		if (senderConnection == null) {
			return;
		}

		Group group = senderConnection.getGroup();

		// TODO configurable
		boolean forceBroadcast = true;
		//noinspection ConstantValue
		if (!forceBroadcast
			&& (group == null
				|| !group.getName().trim().equalsIgnoreCase("broadcast"))) {
			return;
		}

		if (group != null) {
			return;
		}

		event.cancel();

		VoicechatServerApi api = event.getVoicechat();
		List<ServerPlayerEntity> onlinePlayers = minecraftServer.getPlayerManager().getPlayerList();
		ServerPlayerEntity player = (ServerPlayerEntity) senderConnection.getPlayer().getPlayer();
		AbstractTeam team = player.getScoreboardTeam();
		for (ServerPlayerEntity player1 : onlinePlayers) {
			if (player1.getUuid().equals((senderConnection.getPlayer().getUuid()))) {
				continue;
			}

			VoicechatConnection connection = api.getConnectionOf(player1.getUuid());
			if (connection == null) {
				continue;
			}

			// team talk!
			if (MultiFuncChatConfig.get().isTeamSpeak()
				&& team != null
				&& !team.equals(player1.getScoreboardTeam())) {
				continue;
			}

			api.sendStaticSoundPacketTo(connection, event.getPacket().staticSoundPacketBuilder().build());
		}
	}
}
